<?php

namespace Home\Util;

	class TaskUtil{
		private $Tasks = array();
		private $task = array(
				'OID' => '',
				'OWNER' => '',
				'TARGET' => '',
				'REPLY' => '',
				'FEE' => ''
			);
		static private $taskService;
		/**
		 * [__construct]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:42:51+0800
		 * @Detail   :封装的数据类
		 */
		public function __construct($OID,$OWNER,$TARGET,$REPLY,$FEE){
			self::$taskService = Service('Task','task','Home');
			$this->task['OID'] = $OID;
			$this->task['OWNER'] = $OWNER;
			$this->task['TARGET'] = $TARGET;
			$this->task['REPLY'] = $REPLY;
			$this->task['FEE'] = $FEE;
		}
		/**
		 * [addTask]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:42:34+0800
		 * @Detail   :新增的任务流程
		 */
		public function addTask(){
			return self::$taskService->insert($this->task);
		}
		/**
		 * [turnTask]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:45:14+0800
		 * @Detail   :修改目标任务
		 */
		public function turnTask($target,$reply = NULL){
			$task['OID'] = $this->task['OID'];
			$task['TARGET'] = $target;
			if(!is_null($reply)) $task['REPLY'] = $reply;

			return self::$taskService->update($task);
		}
		/**
		 * [finishTask]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:46:03+0800
		 * @Detail   :流程结束,删除并加入Finish
		 */
		public function finishTask($Team,$Driver = NULL,$Service = NULL){
			$model['OID'] = $this->task['OID'];
			$model['TEAM'] = $Team;
			$model['DRIVER'] = $Driver;
			$model['SERVICE'] = $Service;

			if(Service('Task','task_fin','Home')->insert($model)){
				self::$taskService->remove($this->task['OID']);
				return true;
			}
			return false;
		}
		/**
		 * [removeTask]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:48:36+0800
		 * @Detail   :移除相应的Task
		 */
		public function removeTask($oid){
			return self::$taskService->remove($oid);
		}
		/**
		 * [findTask]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:54:58+0800
		 * @Detail   :查找目标任务(单个)
		 */
		public function findTask($oid){
			$this->task = self::$taskService->find($oid);
		}
		/**
		 * [queryTasks description]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:55:20+0800
		 * @Detail   :查找目标任务(例表)
		 */
		public function findTasks($target){
			$this->tasks = self::$taskService->query($target);
		}
		/**
		 * [restartTask]
		 * @Author   :seven
		 * @DateTime :2016-12-02T10:09:14+0800
		 * @Detail   :重启任务
		 */
		public function restartTask(){
			if(Service('Task','task_fin','Home')->remove($this->task['OID'])){
				return self::$taskService->insert($this->task);
			}
			return false;
		}

		public function getTask(){
			return $this->task;
		}

		public function getTasks(){
			return $this->tasks;
		}

		public function __call($method,$args){
			if(strpos($method, 'update') == -1){
				return false;
			}
			$field = substr($method, 6);
			if(strpos($field, '_')){
				$field = explode('_', $field);
				if(count($field) == count($args)){
					$model['UNID'] = $this->order['UNID'];
					foreach ($field as $key => $value) {
						$model[$value] = $args[$key];
					}

					return self::$orderService->update($model);
				}else{
					return false;
				}
			}else{
				$model['UNID'] = $this->order['UNID'];
				$model[$field] = $args[0];
				return self::$orderService->update($model);
			}
		}
	}
